/*
 * @Author: Superficial
 * @Date: 2021-03-15 16:28:54
 * @LastEditTime: 2021-03-16 14:00:56
 * @Description: state
 */

 import { ref, onMounted, watchEffect } from 'vue'
 import { getAccessor } from '@/services/storage'
 
const defaultUserLikeHistory = {
  pages: [],
  comments: []
}

const getUserLikeHistory = () => {
  return getAccessor("user_like_history", defaultUserLikeHistory)
}

export const usePageLike = (articleId) => {
  const isLiked = ref(false)
  let likeHistory = null

  onMounted(() => {
    likeHistory = getUserLikeHistory()
    watchEffect(() => {
      isLiked.value = likeHistory.value.pages.includes(articleId)
    })
  })

  return {
    isLiked,
    like: () => likeHistory.value.pages.push(articleId)
  }
}

export const useCommentsLike = () => {
const userLikeHistory = ref({ ...defaultUserLikeHistory })

onMounted(() => {
  const liveHistory = getUserLikeHistory()
  watchEffect(() => {
    userLikeHistory.value = liveHistory.value
  })
})

  return {
    isLiked: (commentId) => userLikeHistory.value.comments.includes(commentId),
    like: (commentId) => userLikeHistory.value.comments.push(commentId)
  }
}
